﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;

namespace Ooaavee.Data.Model
{
    /// <summary>
    /// A collection of columns.
    /// </summary>
    [CollectionDataContract]
    public sealed class ColumnCollection : Collection<Column>
    {
        [DataMember]
        private readonly Dictionary<string, Column> _map = new Dictionary<string, Column>();

        /// <summary>
        /// Searches for the column with the specified name.
        /// </summary>
        /// <param name="columnName">The String containing the name of the column to get.</param>
        /// <returns>A Column object with the specified name, if found; otherwise a null.</returns>
        public Column this[string columnName]
        {
            get
            {
                Column column;
                if (_map.TryGetValue(columnName, out column))
                {
                    return column;
                }
                return null;
            }
        }

        /// <summary>
        /// Adds a Column to the end of this collection.
        /// </summary>
        /// <param name="item">The Column to be added to the end of this collection.</param>
        public new void Add(Column item)
        {
            base.Add(item);
            _map[item.Name] = item;
        }
    }
}
